﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml.Linq;

namespace Weather
{
    public static class AppGlobal
    {
        private const string m_FilePath = "Breastmate1";

        public static bool InitFinished
        {
            get;
            private set;
        }

        public static void InitProgram()
        {
            InitFinished = false;

            string filename = "Weather.db";
            string filename1 = string.Format("{0}\\{1}\\{2}",
                                             Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), m_FilePath, filename);
            if (!File.Exists(filename1))
            {
                frmWaiting.DoTask(InitApp, "Program initing,please wait.");
            }
            else
            {
                InitFinished = true;
            }
        }

        private static void InitApp()
        {
            string filename = "Calculate.sqlite";

            string filepath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), m_FilePath);

            string filename1 = Path.Combine(filepath, filename);

            if (!Directory.Exists(filepath))
            {
                Directory.CreateDirectory(filepath);
            }

            Stream fileStream;
            Assembly assembly = Assembly.GetExecutingAssembly();
            string BaseNameSpace = "Calculate.{0}";

            fileStream = assembly.GetManifestResourceStream(string.Format(BaseNameSpace, filename));
            if (fileStream != null)
            {
                byte[] listByte = new byte[fileStream.Length];
                fileStream.Read(listByte, 0, (int)fileStream.Length);

                var fs = new FileStream(filename1, FileMode.CreateNew);
                fs.Write(listByte, 0, (int)fileStream.Length);
                fs.Flush();
                fs.Close();
            }

            InitFinished = true;
        }

        private static string m_CurrentDirectory;

        /// <summary>
        /// 程序所在路径
        /// </summary>
        public static string CurrentDirectory
        {
            get
            {
                if (string.IsNullOrEmpty(m_CurrentDirectory))
                {
                    m_CurrentDirectory = Application.ExecutablePath;
                    m_CurrentDirectory = Path.GetDirectoryName(m_CurrentDirectory);
                }
                return m_CurrentDirectory;
            }
        }

        private static XElement m_XMLConfig;
        /// <summary>
        /// 配置文件
        /// </summary>
        public static XElement XMLConfig
        {
            get
            {
                if (m_XMLConfig == null)
                {
                    m_XMLConfig = XElement.Load(CurrentDirectory + "\\System.xml");
                }
                return m_XMLConfig;
            }
        }

        private static string DataBaseDirectory
        {
            get
            {
                string result = string.Empty;
                string filename = "Weather.db";
                if (File.Exists(Path.Combine(CurrentDirectory, filename)))
                {
                    result = Path.Combine(CurrentDirectory, filename);
                }
                else
                {
                    result = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" + m_FilePath + "\\" + filename;
                }

                return result;
            }
        }

        /// <summary>
        /// 数据库连接参数
        /// </summary>
        public static IDictionary<string, string> DataBaseConnection
        {
            get
            {
                var result = new Dictionary<string, string>();
                var root = XMLConfig.Element("Activerecor");
                foreach (XElement item in root.Elements())
                {
                    if (item.Attribute("value").Value.IndexOf("{0}") < 0)
                    {
                        result.Add(item.Attribute("key").Value, item.Attribute("value").Value);
                    }
                    else
                    {
                        result.Add(item.Attribute("key").Value, string.Format(item.Attribute("value").Value, DataBaseDirectory));
                    }
                }
                return result;
            }
        }
    }
}